package com.ljx.itbbs.server.article.event.handler;

import com.ljx.itbbs.server.article.event.CommentPostEvent;
import com.ljx.itbbs.server.article.service.ArticleService;
import com.ljx.itbbs.server.article.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.transaction.event.TransactionPhase;
import org.springframework.transaction.event.TransactionalEventListener;

import java.util.Objects;

/**
 * @Author: LiJunXi
 * @Description:
 * @Date: 2024-03-25  20:44
 */
@Component
public class CommentPostEventHandler {

    @Autowired
    private CommentService commentService;

    @Autowired
    private ArticleService articleService;

    /**
     * 增加根评论的回复数
     * @param event
     */
    @Async
    @TransactionalEventListener(phase = TransactionPhase.BEFORE_COMMIT, classes = CommentPostEvent.class, fallbackExecution = true)
    public void increaseCommentReplyNum(CommentPostEvent event) {
        if (Objects.nonNull(event.getCommentId())){
            commentService.increaseReplyNum(event.getCommentId());
        }
    }

    /**
     * 增加文章评论量
     * @param event
     */
    @Async
    @TransactionalEventListener(phase = TransactionPhase.BEFORE_COMMIT, classes = CommentPostEvent.class, fallbackExecution = true)
    public void increaseArticleCommentNum(CommentPostEvent event) {
        articleService.increaseCommentNum(event.getArticleId());
    }


    // todo 消息推送

}
